package com.catsic.core.convert;

import com.catsic.base.accordion.Accordion;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.entity.AuthArea;
import com.catsic.core.entity.Depart;
import com.catsic.core.entity.Menu;
import com.catsic.core.entity.Organ;
import com.catsic.core.entity.Resource;
import com.catsic.core.entity.Role;
import com.catsic.core.entity.User;
import com.catsic.core.form.AuthAreaForm;
import com.catsic.core.form.DepartForm;
import com.catsic.core.form.MenuForm;
import com.catsic.core.form.OrganForm;
import com.catsic.core.form.ResourceForm;
import com.catsic.core.form.RoleForm;
import com.catsic.core.form.UserForm;
/**
 * 转换类
 * @author yangyudi
 *
 */
public class Converts{
	
	
	/**
	 * 赋值TreeInfo对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static TreeInfo convertTree(Organ organ){  
		TreeInfo treeInfo = new TreeInfo();
		treeInfo.setId(organ.getId());
		treeInfo.setCode(organ.getCode());
		treeInfo.setName(organ.getName());
		treeInfo.setPid(organ.getParent() == null ? null : organ.getParent().getId());
		return treeInfo;
	}
	
	/**
	 * 赋值TreeInfo对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static TreeInfo convertTree(Depart depart){  
		TreeInfo treeInfo = new TreeInfo();
		treeInfo.setId(depart.getId());
		treeInfo.setCode(depart.getCode());
		treeInfo.setName(depart.getName());
		treeInfo.setPid(depart.getParent() == null ? null : depart.getParent().getId());
		return treeInfo;
	}
	
	/**
	 * 赋值TreeInfo对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static TreeInfo convertTree(Menu menu){  
		TreeInfo treeInfo = new TreeInfo();
		treeInfo.setId(menu.getId());
		treeInfo.setCode(menu.getCode());
		treeInfo.setName(menu.getName());
		treeInfo.setPid(menu.getParent() == null ? null : menu.getParent().getId());
		return treeInfo;
	}
	
	/**
	 * 赋值TreeInfo对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static TreeInfo convertTree(Resource resource){  
		TreeInfo treeInfo = new TreeInfo();
		treeInfo.setId(resource.getId());
		treeInfo.setCode(resource.getCode());
		treeInfo.setName(resource.getName());
		if(resource.getMenu() != null){
			treeInfo.setPid(resource.getMenu().getId());
		}
		return treeInfo;
	}
	
	/**
	 * 赋值TreeInfo对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static TreeInfo convertTree(AuthArea authArea){  
		TreeInfo treeInfo = new TreeInfo();
		treeInfo.setId(authArea.getId());
		treeInfo.setCode(authArea.getCode());
		treeInfo.setName(authArea.getName());
		treeInfo.setPid(authArea.getParent() == null ? authArea.getResource().getId() : authArea.getParent().getId());
		treeInfo.setType("authArea");
		return treeInfo;
	}
	

	/**
	 * 赋值OrganForm对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static OrganForm convert(Organ organ){  
		OrganForm organForm = new OrganForm();
		organForm.setId(organ.getId());
		organForm.setName(organ.getName());
		organForm.setCode(organ.getCode());
		if(organ.getParent() != null){
			organForm.setPid(organ.getParent().getId());
		}
		organForm.setJb(organ.getJb());
		return organForm;
	}
	
	/**
	 * 赋值DepartForm对象
	 * @author yangyd
	 * @date 2014-7-16 
	 * @param 
	 * @return
	 */
	public static DepartForm convert(Depart depart){  
		DepartForm departForm = new DepartForm();
		departForm.setId(depart.getId());
		departForm.setCode(depart.getCode());
		departForm.setName(depart.getName());
		if(depart.getParent() != null){
			departForm.setPid(depart.getParent().getId());
		}
		if(depart.getOrgan() != null){
			departForm.setOrganId(depart.getOrgan().getId());
		}
		return departForm;
	}

	/**
	 * convert:赋值RoleForm对象
	 * @author yangyd
	 * @param role
	 * @return
	 **/
	public static RoleForm convert(Role role){
		RoleForm form = new RoleForm();
		form.setId(role.getId());
		form.setName(role.getName());
		if(role.getOrgan() != null){
			form.setOrganId(role.getOrgan().getId());
			form.setOrganName(role.getOrgan().getName());
		}
		return form;
	}
	
	/**
	 * 赋值UserForm对象<br/>
	 * @author yangyd
	 * @date 2014-8-4 
	 * @param 
	 * @return
	 */
	public static UserForm convert(User user){  
		UserForm userForm = new UserForm();
		userForm.setId(user.getId());
		userForm.setIdentity(user.getIdentity());
		userForm.setEmail(user.getEmail());
		userForm.setRealname(user.getRealname());
		userForm.setMobile(user.getMobile());
		//userForm.setPassword(user.getPassword());
		userForm.setUsername(user.getUsername());
		userForm.setStatus(user.getStatus());
		if(user.getDepart() != null){
			userForm.setDepartId(user.getDepart().getId());
			userForm.setDepartName(user.getDepart().getName());
		}
		
		if(user.getOrgan() != null){
			userForm.setOrganId(user.getOrgan().getId());
			userForm.setOrganName(user.getOrgan().getName());
		}
		
		if(user.getPwdLastUpdate() !=null){
			userForm.setPwdLastUpdate(user.getPwdLastUpdate().toDate());
		}
		
		return userForm;
	}
	
	/**
	 * 转换为Accordion
	 * @return
	 */
	public static Accordion accordion(Menu menu){  
		Accordion form = new Accordion();
		form.setId(menu.getId());
		if(menu.getParent() != null){
			form.setPid(menu.getParent().getId());
		}
		form.setText(menu.getName());
		form.setUrl(menu.getPath());
		return form;
	}
	
	/**
	 * 转换为MenuForm
	 * @return
	 */
	public static MenuForm convert(Menu menu){  
		MenuForm form = new MenuForm();
		form.setId(menu.getId());
		if(menu.getParent() != null){
			form.setPid(menu.getParent().getId());
		}
		form.setName(menu.getName());
		form.setCode(menu.getCode());
		form.setLevel(menu.getLevel());
		form.setPath(menu.getPath());
		form.setStatus(menu.getStatus());
		form.setSort(menu.getSort());
		form.setIcon(menu.getIcon());
		if(menu.getDisplayArea() != null){
			form.setDisplayArea(menu.getDisplayArea().getName());
			form.setDisplayAreaCode(menu.getDisplayArea().getCode());
		}
		if(menu.getSystemType() != null){
			form.setSystemType(menu.getSystemType().getName());
			form.setSystemTypeCode(menu.getSystemType().getCode());
		}
		form.setIcon(menu.getIcon());
		return form;
	}
	
	/**
	 * 转换为ResourceForm
	 * @return
	 */
	public static ResourceForm convert(Resource resource){  
		ResourceForm form = new ResourceForm();
		form.setId(resource.getId());
		form.setAssembly(resource.getAssembly());
		form.setJsFunction(resource.getJsFunction());
		if(resource.getAuthArea() != null){
			form.setAuthArea(resource.getAuthArea().getName());
			form.setAuthAreaCode(resource.getAuthArea().getCode());
		}
		form.setIcon(resource.getIcon());
		form.setRevent(resource.getRevent());
		form.setName(resource.getName());
		form.setCode(resource.getCode());
		form.setStatus(resource.getStatus());
		form.setPattern(resource.getPattern());
		form.setViews(resource.getViews());
		if(resource.getMenu() != null){
			form.setMenuName(resource.getMenu().getName());
			form.setMenuCode(resource.getMenu().getCode());
		}
		if(resource.getDrawer() != null){
			form.setDrawerName(resource.getDrawer().getName());
			form.setDrawerCode(resource.getDrawer().getCode());
		}
		return form;
	}
	
	/**
	 * 授权区域form
	 * @param authArea
	 * @return
	 */
	public static AuthAreaForm convert(AuthArea authArea){  
		AuthAreaForm authAreaForm = new AuthAreaForm();
		authAreaForm.setId(authArea.getId());
		authAreaForm.setName(authArea.getName());
		authAreaForm.setCode(authArea.getCode());
		authAreaForm.setStatus(authArea.getStatus());
		if(authArea.getParent() != null){
			authAreaForm.setPid(authArea.getParent().getId());
		}
		if(authArea.getResource() != null){
			authAreaForm.setResourceCode(authArea.getResource().getCode());
			authAreaForm.setResourceName(authArea.getResource().getName());
		}
		return authAreaForm;
	}
}
